package com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.strategy.controller;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.wxy.ej.flow.modules.taskflow.dto.DataDto;
import com.wxy.ej.flow.modules.taskflow.dto.NodeDto;
import com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.NodeStrategy;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ControllerBreakNodeStrategy extends NodeStrategy {
    public static final String BreakFlag = "MatchBreakConditionInCircle";

    @Override
    public Result action(NodeDto node, JSONObject flowDataJson, DataDto dataDto) throws Exception {
        String breakCondition = node.getBreakCondition();
        log.info("ControlBreakNodeStrategy: break condition: {}", node.getBreakCondition());
        dataDto.getLogList().add("条件："+node.getBreakCondition());

        boolean b = false;

        if (StringUtils.isNull(breakCondition)) {
            b = false;
        }
        else if ("false".equalsIgnoreCase(breakCondition)) {
            b = false;
        }
        else if ("true".equalsIgnoreCase(breakCondition)) {
            b = true;
        }else{
            b = (Boolean) executeScriptForResult(dataDto, breakCondition);
        }
        // 中断条件成立
        log.info("中断条件判断结果：{}", b);
        dataDto.getLogList().add("执行结果："+b);
        if (b) {
            if (dataDto.isInCircle()) {
                return Result.builder(BreakFlag).success("中断条件符合，continue");
            } else {
                return Result.builder(BreakFlag).fault(node.getReturnContent());
            }
        }

        return Result.builder();
    }

}
